package com.ts.crawler.selenium;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.server.handler.SendKeys;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.selector.PlainText;

import java.util.Map;

public class FlightCtripDownLoader implements Downloader {

    private RemoteWebDriver driver;

    private static final String targetUrl = "https://www.ctrip.com";
    private Map<String,String> params=null;

    public FlightCtripDownLoader(Map<String,String> params) {
        System.setProperty("webdriver.chrome.driver","E:\\static\\chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
        //chromeOptions.addArguments("--headless");
        chromeOptions.addArguments("--windows-size=1920,1080");
        driver = new ChromeDriver(chromeOptions);
        this.params=params;
    }

    public Page download(Request request, Task task) {
        try {
            driver.get(targetUrl);
            Thread.sleep(2000);
            loadPageByParams(params);
            driver.findElementById("FD_StartSearch").click();
            Thread.sleep(2000);
            Integer start = 0;
            Integer end = 500;
            while (true){
                if(end==5000){
                    break;
                }
                String scriptStr ="window.scrollTo("+start+","+end+")";
                driver.executeScript(scriptStr);
                Thread.sleep(2000);
                start+=500;
                end+=500;
            }

            String htmlStr = driver.getPageSource();
            return create(htmlStr,driver.getCurrentUrl());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;
    }

    public void setThread(int i) {

    }

    private Page create(String htmlStr, String currentUrl) {
        Page page = new Page();
        page.setRawText(htmlStr);
        page.setUrl(new PlainText(currentUrl));

        Request request = new Request(currentUrl);
        page.setRequest(request);
        return page;
    }

    public void loadPageByParams(Map<String,String> params){
        //S D M
        String flightWay =params.get("flightWay");
        //Domestic International
        String flightType =params.get("flightType");
        if ("S".equals(flightWay)&&"D".equals(flightType)){
            DomesticPageBase(params);
        }else if ("D".equals(flightWay)&&"D".equals(flightType)){
            FlightPageDD(params);
        }
    }

    /**
     * 国内往返票
     * @param params
     */
    public void FlightPageDD(Map<String,String> params){

        String endDate =params.get("endDate");
        try {
            driver.findElementByXPath("//*[@id=\"FD_flightSubSwitch\"]/label[2]/input").click();
            Thread.sleep(1000);
            DomesticPageBase(params);
            WebElement fd_returnDate = driver.findElementById("FD_ReturnDate");
            fd_returnDate.clear();
            fd_returnDate.sendKeys(endDate);
            Thread.sleep(1000);
            driver.findElementById("FD_StartDateSpan").click();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    /**
     * 国内单程票
     * @param params
     */
    public void DomesticPageBase(Map<String,String> params){
        String startCity = params.get("startCity");
        String DestCity = params.get("destCity");
        String startDate = params.get("startDate");
        boolean withChild = "true".equalsIgnoreCase(params.get("withChild"));
        boolean withInfant = "true".equalsIgnoreCase(params.get("withInfant"));
        //Y_S C_F
        String cabin = params.get("cabin");
        try {
            driver.findElementByXPath("//*[@id='searchBoxUl']/li[@styletag='flight']").click();
            Thread.sleep(2000);
            if (withChild){
                driver.findElementById("FD_HasChild").click();
            }
            if (withInfant){
                driver.findElementById("FD_HasBaby").click();
            }
            Thread.sleep(1000);
            if("C_F".equals(cabin)){
                driver.findElementById("FD_ClassLevel").click();
                Thread.sleep(1000);
                driver.findElementByXPath("//*[@id=\"FD_ClassLevel\"]/option[@value='C_F']").click();
                Thread.sleep(1000);
            }
            //填写开始地点
            WebElement fd_startCity = driver.findElementById("FD_StartCity");
            fd_startCity.clear();
            fd_startCity.sendKeys(startCity);
            Thread.sleep(2000);
            //填写到达地点
            WebElement fd_destCity = driver.findElementById("FD_DestCity");
            fd_destCity.clear();
            fd_destCity.sendKeys(DestCity);
            Thread.sleep(2000);
            //填写日期
            WebElement fd_startDate = driver.findElementById("FD_StartDate");
            fd_startDate.clear();
            fd_startDate.click();
            fd_startDate.sendKeys(startDate);
            driver.findElementById("FD_StartDateSpan").click();
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
